(* file: lexer.mll *)

{
  open Turtle
  open Lexing
  open Hashtbl

  let basic_cmd = Hashtbl.create 2
  let _ =
    List.iter (fun (kwd, tok) -> Hashtbl.add basic_cmd kwd tok)
      [ 
	"true",  TRUETOK;
	"false", FALSETOK;
	
	"MOVE" , MOVETOK;
	"TURN" , TURNTOK;
	"CALL" , CALLTOK;
	"IF"   , IFTOK;
	"WHILE", WHILETOK;

	"VAR",     VARTOK;
	"PROC",    PROCTOK;
	"PROCREC", PROCRECTOK;
	"FUN",     FUNTOK;
	"FUNREC",  FUNRECTOK;
      ]
}

let digits = ['0'-'9']+

rule token = parse
  | [' ' '\t' '\n' ]	{ token lexbuf }

  | '['                 { LEFTBRACKET  }
  | ']'                 { RIGHTBRACKET }
  | ';'                 { SEMICOLON    }
  | '('                 { LEFTPAR      }
  | ')'                 { RIGHTPAR     }

  | ":="                { ASSTOK       }

  | '+'                 { PLUS  }
  | '-'                 { MINUS }
  | '*'                 { MULT  }
  | '/'                 { DIV   }

  | '='                 { EQUAL }
  | '<'                 { INF   }
  | '>'                 { SUP   }

  | digits as num       { NUM(float_of_string num) }
  
  | ['A'-'Z' 'a'-'z'] ['A'-'Z' 'a'-'z' '0'-'9' '_']* as id
      { try 
	  Hashtbl.find basic_cmd id
	with Not_found -> 
	  IDENT id
      }
  
  | eof 		{ token lexbuf }
  | _		        { token lexbuf }
  
